#pragma once

#include "MemList.h"

struct MemChunk
{
    unsigned int nLength;
    void *pStart;
    void *pEnd;
    void *pCurPos;
};

struct UnfixedChunk
{
    unsigned int nLength;
};

typedef CMemList<MemChunk, CSpinLock> CChunkList;
typedef CChunkList::Node ChunkNode;

typedef CMemList<UnfixedChunk, CSpinLock> CUnfixedList;
typedef CUnfixedList::Node UnfixedNode;

class CMemChunks
{
public:
    CMemChunks();
    ~CMemChunks();

    // 不做任何操作,只为调用析构函数,释放CChunkList中占用的内存
    void operator delete(void *ptr) {}

public:
    void *Alloc(unsigned int nLength, BlockType bt = SMALL_BLOCK);
    bool Free(void *ptr, BlockType bt);
    void Destroy();

private:
    ChunkNode *AddNewChunk();
    void *ChunkNodeAllocator(int iLen);
    void ChunkNodeDeleter(ChunkNode *pNode);
    UnfixedNode *AddUnfixedNode(unsigned int iLen);
    bool DelUnfixedNode(UnfixedNode *pNode);
    void *UnfixedNodeAllocator(int iLen);
    void UnfixedNodeDeleter(UnfixedNode *pNode);

private:
    CChunkList m_lstChunk;
    CUnfixedList m_lstUnfixed;
};
